<?php
# 文件名称:user.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_user
{
	var $id				= null;
	
	var $gid			= null;
	var $admin_id		= null;
	
	var $admin_pass		= null;
	var $admin_name		= null;	
	var $admin_sex		= null;	
	
	var $admin_tel		= null;
	var $admin_mobile   = null;
	
	var $admin_email	= null;
	var $admin_qq		= null;
	
	var $admin_msn		= null;
	
	var $admin_taobao	= null;
	
	var $admin_introduction		= null;
	var $admin_login	= 0;
	
	var $admin_modify_ip		= null;
	
	var $admin_modify_date		= null;
	
	var $admin_register_date	= null;
	
	var $admin_approval_date	= null;
	
	var $block					= null;
	
	var $groups;
	
	var $params;
	/**
	 * 错误信息
	 */
	var $_errorMsg	= null;

	/**
	* 初始化
	*
	*/
	function C_user($identifier = 0)
	{
		// 已存在
		if (!empty($identifier)) {
			$this->load($identifier);
		}
		else
		{
			//初始化
			$this->id        = 0;
			$this->gid       = 0;
		}
	}
	/**
	 * 当不存在时创建
	 *
	 */
	function &getInstance($id = 0)
	{
		static $instances;
		if (!isset ($instances)) {
			$instances = array ();
		}
		// 用户id		
		if(!is_numeric($id))
		{
			if($path = dirname(__FILE__).DS.'helper'.'.php')
			{
				require_once $path;
				if (!$id = C_userHelper::getUserId($id)) {
					$retval = false;
					return $retval;
				}
			}else
			{
				return false;
			}
			
		}
		if (empty($instances[$id])) {
			$user = new C_user($id);
			$instances[$id] = $user;
		}
		return $instances[$id];
	}
	/**
	 * 设置最后访问日期
	 *
	 */
	function setLastVisit($timestamp=null)
	{
		// 打开用户表
		$table 	=& $this->getTable();
		$table->load($this->id);
		return $table->setLastVisit($timestamp);
	}
	/**
	 * 获取用户表
	 *
	 */
	function &getTable( $type = null, $prefix = 'C_table' )
	{
		static $tabletype;
		if(!isset($tabletype)) {
			$tabletype['name'] 		= 'user';
			$tabletype['prefix']	= 'C_table';
		}
		if(isset($type)) {
			$tabletype['name'] 		= $type;
			$tabletype['prefix']	= $prefix;
		}
		$table 	=& C_table::getInstance( $tabletype['name'], $tabletype['prefix'] );
		return $table;
	}
	function bind(& $array)
	{
		if($path = dirname(__FILE__).DS.'helper'.'.php')
		{
			require_once $path;
		}else
		{
			return false;
		}
		// 检测
		if (empty($this->id))
		{
		
			// 密码是否为空
			if (empty($array['password'])) {
				return false;
			}
			if ($array['password'] != $array['password2']) {
					return false;
			}

			$salt  = C_userHelper::genRandomPassword(32);
			$crypt = C_userHelper::getCryptedPassword($array['password'], $salt);
			$array['admin_pass'] = $crypt.':'.$salt;
			// 注册时间
			
			C_loader::helper('date');
			$this->set( 'admin_register_date', standard_date() );
			// 用户名检测
			$admin_id = $this->get( 'admin_id' );
			if ( strlen($admin_id) > 150 )
			{
				$admin_id = substr( $admin_id, 0, 150 );
				$this->set( 'admin_id', $admin_id );
			}
			// 密码检测
			$admin_pass = $this->get( 'admin_pass' );
			if ( strlen($admin_pass) > 100 )
			{
				$password = substr( $admin_pass, 0, 100 );
				$this->set( 'admin_pass', $admin_pass );
			}
			
		}
		else
		{
			// 更新密码
			if (!empty($array['password']))
			{
				if ( $array['password'] != $array['password2'] ) {
					return false;
				}
				$salt = C_userHelper::genRandomPassword(32);
				$crypt = C_userHelper::getCryptedPassword($array['password'], $salt);
				$array['admin_pass'] = $crypt.':'.$salt;
			}
		}

		// 绑定
		if (!$this->setProperties($array)) {
			return false;
		}
		$this->id = (int) $this->id;
		return true;
	}
	/**
	 * 存入数据库
	 *
	 */
	function save( $updateOnly = false )
	{
		$table 	=& $this->getTable();
		$table->bind($this->getProperties());
		
		if (!$table->check()) {
			return false;
		}
		$isnew = !$this->id;
		if ($isnew && $updateOnly) {
			return true;
		}
		if (!$result = $table->store()) {
			$this->setError($table->getError());
			return false;
		}
		if (empty($this->id)) {
			$this->id = $table->get( 'id' );
		}
		return $result;
	}
	/**
	 * 从数据库中删除
	 *
	 */
	function delete( )
	{
		$table 	=& $this->getTable();
		$result = false;
		if (!$result = $table->delete($this->id)) {
		}
		return $result;
	}
	/**
	 * 通过用户ID加载用户对象
	 *
	 */
	function load($id)
	{
		// 数据表对象
		$table 	=& $this->getTable();
		 if(!$table->load($id)) {
			return false;
		}

		$this->setProperties($table->getProperties());
		return true;
	}
	function getProperties( $public = true )
	{
		$vars  = get_object_vars($this);
        if($public)
		{
			foreach ($vars as $key => $value)
			{
				if ('_' == substr($key, 0, 1)) {
					unset($vars[$key]);
				}
			}
		}
        return $vars;
	}
	function setProperties( $properties )
	{
		$properties = (array) $properties; 
		if (is_array($properties))
		{
			foreach ($properties as $k => $v) {
				$this->$k = $v;
			}
			return true;
		}
		return false;
	}
	function get($property, $default=null)
	{
		if(isset($this->$property)) {
			return $this->$property;
		}
		return $default;
	}
	function set( $property, $value = null )
	{
		$previous = isset($this->$property) ? $this->$property : null;
		$this->$property = $value;
		return $previous;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>